package com.yylending.las.common.utils;

import android.text.TextUtils;
import com.yyfax.common.CLog;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class FormatUtils {
	private static final String TAG = "FormatUtils";
	private static final String REG_NUM_WITH_ASTERISK = "(?<=[\\d]{3})\\d(?=[\\d]{4})";

	/**
	 * 将手机号码或身份证除前三后四个数字外,加*隐藏
	 *
	 * @param originNum 初始数字串
	 * @return 加*字串
	 */
	public static String getNumWithAsterisk(String originNum) {
		String numWithAsterisk;
		numWithAsterisk = originNum.replaceAll(REG_NUM_WITH_ASTERISK, "*");
		return numWithAsterisk;
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx.xxxx形式
	 *
	 * @param number         原始金额数字
	 * @param fractionLength 保留小数位数
	 * @return 格式化后的数字
	 */
	public static String getFormatMoney(Number number, int fractionLength) {
		String newNumber = "";
		try {
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setMaximumFractionDigits(fractionLength);
			numberFormat.setMinimumFractionDigits(fractionLength);
			newNumber = numberFormat.format(number);
		} catch (Exception e) {
			CLog.e(TAG, e.getMessage());
		}
		return newNumber;
	}

	/**
	 * 截取身份证后四位
	 *
	 * @param idCard 身份证号
	 * @return 身份照后四位
	 */
	public static String getIdCardTail(String idCard) {
		if (TextUtils.isEmpty(idCard)) {
			return "";
		}
		String result;
		if (idCard.length() <= 4) {
			result = idCard;
		} else {
			result = idCard.substring(idCard.length() - 4);
		}
		return result;
	}

	/**
	 * 身份证部分位置的字符替换为指定字符
	 *
	 * @param startIndex  开始位置
	 * @param endIndex    结束位置
	 * @param replaceChar 替换的字符
	 * @param idCard      将要被替换的字符串
	 * @return 已被替换的字符串
	 */
	public static String hiddenPartStr(int startIndex, int endIndex, String replaceChar, String idCard) {
		StringBuffer str = new StringBuffer(idCard.substring(0, startIndex));
		for (int i = 0; i < (endIndex - startIndex); i++) {
			str.append(replaceChar);
		}
		str.append(idCard.substring(endIndex));
		return str.toString();
	}

	/**
	 * 返回yyyy.MM.dd格式的日期字符串
	 *
	 * @param dateStr 日期字符串
	 * @return yyyy.MM.dd
	 */
	public static String formatDateSplitByDot(String dateStr) {
		if (TextUtils.isEmpty(dateStr)) {
			return "";
		}
		return dateStr.replace("/", ".");
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx.xx形式
	 */
	public static String getFormatMoney(Number number) {
		String s = String.valueOf(number);
		return getFormatMoney(s);
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx.xx形式
	 */
	public static String getFormatMoney(String number) {
		String numString = number;
		try {
			if (TextUtils.isEmpty(number)) {
				numString = "0.00";
			} else {
				if (number.contains(".")) {
					if (number.indexOf(".") == number.length() - 1) {//XXXX.
						numString = getDecoratedAmount(number.substring(0, number.length() - 1)) + ".00";
					} else {
						//对XXXX.X...进行四舍五入
						double bg = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						String[] nums = String.valueOf(bg).split("\\.");
						if (nums[1].length() == 1) {
							nums[1] += "0";
						}
						numString = getDecoratedAmount(nums[0]) + "." + nums[1];
					}
				} else {
					numString = getDecoratedAmount(number) + ".00";//XXXX

				}
			}
		} catch (Exception e) {
		}
		return numString;
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx形式
	 */
	public static String getDecoratedAmount(String value) {
		try {
			if (value.length() <= 3)
				return value;
			String numberPattern = "###,###,###";
			DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
			formatter.applyPattern(numberPattern);
			String result = formatter.format(Float.valueOf(value));
			return result;
		}catch (Exception e){
			CLog.e(TAG, e.getMessage());
		}
		return value;
	}

	/**
	 * 将数字金额格式化为 xxx,xxx,xxx形式
	 */
	public static String getDecoratedAmount(Number value) {
		String s = String.valueOf(value);
		return getDecoratedAmount(s);
	}

	/**
	 * 将数字金额格式化为 ¥xxx,xxx,xxx形式
	 */
	public static String getDecoratedAmountWithSymbol(String number) {
		return "¥" + getDecoratedAmount(number);

	}

	/**
	 * 将数字金额格式化为 ¥xxx,xxx,xxx形式
	 */
	public static String getDecoratedAmountWithSymbol(Number number) {
		String s = String.valueOf(number);
		return getDecoratedAmountWithSymbol(s);
	}

	/**
	 * 将数字金额格式化为¥xxx,xxx,xxx.xx形式
	 */
	public static String getAmountWithSymbol(Number number) {
		String s = String.valueOf(number);
		return "¥" + getFormatMoney(number);
	}

	/**
	 * 将数字金额格式化为¥xxx,xxx,xxx.xx形式
	 */
	public static String getAmountWithSymbol(String number) {
		String s = String.valueOf(number);
		return "¥" + getFormatMoney(number);
	}

	/**
	 * 格式化数字(保留两位小数)
	 *
	 * @param money
	 * @return
	 */
	public static String formatNumTwo(double money) {
		DecimalFormat format = new DecimalFormat("0.00");
		return format.format(money);
	}

	/**
	 * 格式化身份证有效期
	 *
	 * @param validDate
	 * @return
	 */
	public static String formatIdCardValidDate(String validDate) {
		if(TextUtils.isEmpty(validDate)){
			return "";
		}else{
			if(validDate.length() == 8){
				StringBuilder  sb = new StringBuilder(validDate);
				sb.insert(6, ".");
				sb.insert(4, ".");
				return sb.toString();
			}else{
				return validDate;
			}
		}
	}

	/**
	 * 身份证号脱敏
	 * @param text
	 * @return
	 */
	public static CharSequence displayIDNumber(CharSequence text) {
		if(TextUtils.isEmpty(text)){
			return "";
		}
		if (text.toString().contains("*")) {
			return text;
		}
		String string = text.toString().trim().replaceAll(" ", "");
		if(string.length() < 15){
			return string;
		}
		return string.substring(0, 4) + "******" + string.substring(string.length() - 4, string.length());
	}
}
